package com.songlanyun.modules.rabbitmq.interactmsg.impl;

import com.songlanyun.common.enums.GoodsConstant;
import com.songlanyun.common.enums.PrivateMessageConstant;
import com.songlanyun.common.utils.push.GeTuiUtils;
import com.songlanyun.common.utils.push.Msg;
import com.songlanyun.modules.message.entity.PrivateMessage;
import com.songlanyun.modules.message.model.GoodsMessage;
import com.songlanyun.modules.message.model.PayMessage;
import com.songlanyun.modules.message.model.to.MessageTO;
import com.songlanyun.modules.message.model.to.PrivateMessageTO;
import com.songlanyun.modules.message.service.PrivateMessageService;
import com.songlanyun.modules.payorder.entity.PayOrder;
import com.songlanyun.modules.payorder.service.PayOrderService;
import com.songlanyun.modules.rabbitmq.interactmsg.IInteractMsgService;
import com.songlanyun.modules.refillorder.entity.SaleRefillCardOrderEntity;
import com.songlanyun.modules.refillorder.service.SaleRefillCardOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 充值卡支付成功  --  支付消息
 */
@Service("refillInteractService")
public class RefillInteractServiceImpl implements IInteractMsgService {

    @Resource
    private PayOrderService payOrderService;

    @Resource
    private SaleRefillCardOrderService saleRefillCardOrderService;

    @Resource
    private PrivateMessageService privateMessageService;

    @Resource
    private GeTuiUtils geTuiUtils;

    @Override
    public void savePrivateMsg(MessageTO to) {
        PayOrder payOrder = payOrderService.getById(to.getModelId());
        if (payOrder == null) {
            return;
        }
        //充值卡订单
        SaleRefillCardOrderEntity order = saleRefillCardOrderService.getByPayOrderCode(payOrder.getCode(), false);
        if (order == null) {
            return;
        }
        String remark = String.format("你已购买1个充值卡，支付%s元，点击查看详情", order.getFactCost());
        List<GoodsMessage> goodsList = new ArrayList<>();

        //增加子订单中的商品
        GoodsMessage goodsMessage = new GoodsMessage();
        goodsMessage.setGoodsId(order.getRefillCardId());//充值卡ID
        goodsMessage.setGoodsName(order.getRefillCardName());//充值卡名称
        goodsList.add(goodsMessage);

        //发送消息
        PayMessage payMessage = new PayMessage(order.getId(), GoodsConstant.GoodsType.REFILL_GOODS, 1, order.getFactCost(), goodsList);

        PrivateMessageTO privateMessageTO = new PrivateMessageTO(order.getUserId(), PrivateMessageConstant.MessageType.PAY_MESSAGE, payMessage, remark);
        PrivateMessage privateMessage = new PrivateMessage(privateMessageTO);
        privateMessageService.save(privateMessage);

        //单推
        geTuiUtils.pushByUserId(
                Msg.builder()
                        .uid(order.getUserId())
                        .title(PrivateMessageConstant.MessageType.PAY_MESSAGE.getMsg())
                        .content(remark)
                        .msg(privateMessage.pushMessageBody())
                        .build()
        );
    }
}
